<?php
/**
 * 权限管理
 *
 * @author 孙晓晔
 * @version $Id: Priv.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Priv extends InfoUtil {
	public $tbl = Constant::tbl_priv;

	public $status = array(
		'-' => '请选择',
		'1' => '已开通',
		'2' => '未开通',
		'3' => '公共',
		'4' => '模块',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'cc' => '模块',
		'action' => '行为',
		'orderby' => '排序',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';
			
		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('uri_cc', Common::cc());
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/priv/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$where = " where 1 = 1";
		if (!empty($request['name_s'])) {
			$where .= " AND a.name like '" . $request['name_s'] . "%'";
		}

		if (!empty($request['cc_s']) && $request['cc_s'] <> '-') {
			$where .= " AND a.cc = '" . $request['cc_s'] . "'";
		}

		if (!empty($request['action_s'])) {
			$where .= " AND action like '" . $request['action_s'] . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['role'] = $this->_role($row['cc'], $row['action']);
				$row['num_'] = $this->_num($row['cc'], $row['action']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/priv/list');
		return true;
	}
	
	function _role($cc, $action) {
		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$aa = new RolePriv();
		
		return $aa->_cc_action($cc, $action);
	}
	
	function _num($cc, $action) {
		require_once(dirname(__FILE__) . '/../emp/EmpPriv.class.php');
		$aa = new EmpPriv();
		
		return $aa->_cc_action($cc, $action);
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/priv/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		clean($this->tbl . "_status_" . $status);

		$name = $request['name'];
		$cc	= $request['cc'];
		$action	= $request['action_'];
		$content = addslashes($request['content']);

		$status = $request['status'];		
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, cc, action, content, oper, status, createtime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', now())";
		$sql = sprintf($fmt, $this->tbl, $name, $cc, $action, $content, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 编辑
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));
		
		$controller->display($request, 'info/priv/edit');
		return true;
	}

	/**
	 * 编辑修改
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
			
		$name  = $request['name'];
		$cc = $request['cc'];
		$action = $request['action_'];
		$orderby = $request['orderby'];
		$content = addslashes($request['content']);

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET name = '%s', cc = '%s', action = '%s', content = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $cc, $action, $content, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'info/priv/copy');
		return true;
	}

	/**
	 * 
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'info/priv/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY cc, orderby";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$priv = $this->id($this->tbl, $id);
	
		$controller->assign_by_ref('priv', $priv);
	
		require_once(dirname(__FILE__) . '/../emp/Emp.class.php');
		$aa = new Emp();
		
		$emp = $aa->_list();
		
		require_once(dirname(__FILE__) . '/../emp/EmpPriv.class.php');
		$bb = new EmpPriv();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($emp)) {
			$row['no'] = $no++;
				
			if ($bb->_emp_priv($row['id'], $priv['cc'], $priv['action'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'info/priv/emp');
		return true;
	}
	
	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$id = $request['id'];
	
		$priv = $this->id($this->tbl, $id);
	
		require_once(dirname(__FILE__) . '/../emp/EmpPriv.class.php');
		$aa = new EmpPriv();
		
		$aa->_reset($priv);
	
		$list = $request['list'];
		if (empty($list)) {
			return false;
		}
	
		$bb = explode(',', $list);
		while (list($k, $emp) = each($bb)) {
			$aa->_update($emp, $id);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function role($controller, $request) {
		$this->pretreat($controller);
			
		$id = $request['id'];
		$priv = $this->id($this->tbl, $id);
	
		$controller->assign_by_ref('priv', $priv);
	
		require_once(dirname(__FILE__) . '/Role.class.php');
		$aa = new Role();
		
		$role = $aa->_list();
		
		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$bb = new RolePriv();
		
		$data = array();
		$no = 1;
		while (list($k, $row) = each($role)) {
			$row['no'] = $no++;
	
			if ($bb->_priv_role($row['id'], $priv['cc'], $priv['action'])) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}
	
			$data_all[] = $row;
		}
		$controller->assign_by_ref('result', $data);
		$controller->assign_by_ref('result_all', $data_all);
	
		$controller->display($request, 'info/priv/role');
		return true;
	}
	
	/**
	 * 权限分配
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function rolePost($controller, $request) {
		$id = $request['id'];
	
		$priv = $this->id($this->tbl, $id);
	
		require_once(dirname(__FILE__) . '/RolePriv.class.php');
		$aa = new RolePriv();
		
		$aa->_reset_priv($priv);
	
		$list = $request['list'];
		if (empty($list)) {
			return false;
		}
	
		$bb = explode(',', $list);
		while (list($k, $role) = each($bb)) {
			$aa->_update($role, $id);
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_action($controller, $request) {
		$q = $request['q'];
		$data = $this->_ajax_action($q);
		echo json_encode($data);
	}

	function _ajax_action($q) {
		$key = "priv.ajax_action_" . $q;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE action like '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = $db->fetch_all($result);

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		return $ret;
	}
}
?>
